var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = {
  init(
    end_contents,
    ad_contents,
    has_next_contents,
    if_current_ad_is_over,
    if_in_task,
    if_in_home,
    max_ad_times
  ) {
    var logger = utils.create_logger("in_task init: ");
    logger("");
    var end_contents = end_contents || ["开心收下", "立即收下"];
    for (var i = 0; i < end_contents.length; i++) {
      var end_content = end_contents[i];
      if (utils.click_text_starts_with(end_content)) {
        logger("找到-" + end_content + "-");
        sleep(2 * 1000);
        this.next();
        return;
      }
    }
    var end_contents = end_contents || ["开心收下", "立即收下"];
    for (var i = 0; i < end_contents.length; i++) {
      var end_content = end_contents[i];
      var ocr_text = ocr_utils.screen_exists_text_one(end_content, false);
      if (ocr_text) {
        utils.click_ocr_text(ocr_text);
        logger("找到 ocr_text-" + end_content + "-");
        sleep(2 * 1000);
        this.next();
        return;
      }
    }
    var ad_contents = ad_contents || [
      "看广告再",
      "看广告得",
      "观看视频再得",
      "看视频再领",
    ];
    for (var i = 0; i < ad_contents.length; i++) {
      var ad_content = ad_contents[i];
      if (utils.click_text_starts_with(ad_content)) {
        logger("找到-" + ad_content + "-");
        sleep(utils.get_random_interval(1200, 200, 1200, 1));
        ad.has_watch_ad_times = 0;
        ad.max_ad_times = 1000;
        ad.init(
          if_current_ad_is_over,
          if_in_task,
          if_in_home,
          has_next_contents,
          max_ad_times
        );
        if (ad.in_task) {
          this.init();
          return;
        }
        this.after();
        return;
      }
    }
    this.after();
  },
  after() {
    console.log("in_task after");
    this.next();
  },
  next() {
    console.log("in_task next");
  },
};

module.exports = in_task;
